home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / QSORT-EX.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  2KB  |  59 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-07-93 (18:20)             Number: 177
  4. From: ALLAN NELSON                 Refer#: NONE
  5.   To: GEOFFREY LIU                  Recvd: NO  
  6. Subj: Re: Problems with qsort        Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.  -=> Quoting Geoffrey Liu to All <=-
  9.  
  10.  GL> I hope someone here can point out what I'm doing wrong. I'm
  11.  GL> trying to sort a structure with one of the elements as the "item"
  12.  GL> being sorted. Here's the code:
  13.  
  14.  #include <stdio.h>
  15.  #include <stdlib.h>
  16.  #include <string.h>
  17.  
  18.  int sort_function( const void *a, const void *b);
  19.  struct test {
  20.  char zip;
  21.  char list2[4];
  22.  char nothing;
  23.  } list[5] = { 0,"cat",0,0, "car",0, 0,"cab",0, 0,"cap",0, 0,"can",0 };
  24.  typedef struct test *tptr;
  25.  
  26.  int main(void) {
  27.  int x;
  28.  
  29.  qsort((void *)list, 5, sizeof(list[0]), sort_function);
  30.  for (x = 0; x < 5; x++)
  31.  printf("%s\n", list[x].list2);
  32.  return 0;
  33.  }
  34.  
  35.  int sort_function( const void *ab, const void *bc){
  36.     tptr t_a, t_b;
  37.  
  38.     printf ("ab: %p bc: %p ",ab,bc);
  39.     t_a = ab;
  40.     t_b = bc;
  41.     return( strcmp(t_a->list2, t_b->list2));
  42.  }
  43.  
  44.  The problem is that you have passed pointers of type void and what
  45.  the compiler wants is pointers to structures.  This changed code
  46.  works with warnings under BC++v3.1.  You could clean up the problem
  47.  by passing pointers to structures to sort_function.
  48.  
  49.  
  50.  
  51. ... "Scotty, beam me up another Blue Wave message."
  52. ___ Blue Wave/QWK v2.12
  53.  
  54. --- Maximus/2 2.01wb
  55.  * Origin: The Programmers Retreat (1:382/17)
  56. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  57. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  58. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  59.